package com.globallogic.learning.kozius.actor;

import java.util.ArrayList;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.List;


/**
 * Superclass for all actors
 * <p/>
 * Created by sergii.koziumynskyi on 2/4/14.
 */
public class People {

    public People(String firstName, String lastName, int yearOfBirth, int monthOfBirth, int dayOfBirth,
                  String placeOfBirth) {
        this.firstName = firstName;
        this.lastName = lastName;

        this.placeOfBirth = placeOfBirth;
        GregorianCalendar calendar = new GregorianCalendar(yearOfBirth, monthOfBirth - 1, dayOfBirth);
        this.dateOfBirth = calendar.getTime();

    }

    public People(String firstName, String lastName) {
        this.firstName = firstName;
        this.lastName = lastName;
    }

    public People() {
    }

    public String getFirstName() {
        return firstName;
    }

    public String getLastName() {
        return lastName;
    }

    public String getFullName() {
        return firstName + " " + lastName;
    }

    public List<Contact> getContacts() {
        return contacts;
    }

    public List<Contact> getWorkContacts() {
        List<Contact> result = new ArrayList<Contact>();
        for (Contact o : contacts) {
            if (o.isWork()) result.add(o);
        }
        return result;
    }

    public List<Contact> getHomeContacts() {
        List<Contact> result = new ArrayList<Contact>();
        for (Contact o : contacts) {
            if (!o.isWork()) result.add(o);
        }
        return result;
    }

    public Date getDateOfBirth() {
        return (Date) dateOfBirth.clone();
    }

    public String getPlaceOfBirth() {
        return placeOfBirth;
    }

    public void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    public void setLastName(String lastName) {
        this.lastName = lastName;
    }

    public void setDateOfBirth(Date dateOfBirth) {
        this.dateOfBirth = dateOfBirth;
    }

    public void setPlaceOfBirth(String placeOfBirth) {
        this.placeOfBirth = placeOfBirth;
    }

    public void setContact(String address, String city, String phone, boolean isWork) {
        contacts.add(new Contact(address, city, phone, isWork));
    }

    public void setContacts(ArrayList<Contact> contacts) {
        this.contacts = contacts;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (!(o instanceof People)) return false;

        People people = (People) o;

        if (contacts != null ? !contacts.equals(people.contacts) : people.contacts != null) return false;
        if (dateOfBirth != null ? !dateOfBirth.equals(people.dateOfBirth) : people.dateOfBirth != null) return false;
        if (firstName != null ? !firstName.equals(people.firstName) : people.firstName != null) return false;
        if (lastName != null ? !lastName.equals(people.lastName) : people.lastName != null) return false;
        if (placeOfBirth != null ? !placeOfBirth.equals(people.placeOfBirth) : people.placeOfBirth != null)
            return false;

        return true;
    }

    @Override
    public int hashCode() {
        int result = firstName != null ? firstName.hashCode() : 0;
        result = 31 * result + (lastName != null ? lastName.hashCode() : 0);
        result = 31 * result + (placeOfBirth != null ? placeOfBirth.hashCode() : 0);
        result = 31 * result + (dateOfBirth != null ? dateOfBirth.hashCode() : 0);
        result = 31 * result + (contacts != null ? contacts.hashCode() : 0);
        return result;
    }

    @Override
    public String toString() {
        return "People{" +
                "firstName='" + firstName + '\'' +
                ", lastName='" + lastName + '\'' +
                ", placeOfBirth='" + placeOfBirth + '\'' +
                ", dateOfBirth=" + dateOfBirth +
                ", contacts=" + contacts +
                '}';
    }

    private String firstName = "";
    private String lastName = "";
    private String placeOfBirth = "";
    private Date dateOfBirth = new Date();
    private List<Contact> contacts = new ArrayList<Contact>();
}
